package com.sbp.message.service;

import com.alibaba.fastjson.JSONObject;
import com.dap.api.ISunriseService;
import com.dap.param.StringInput;
import com.dap.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SmsServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(SmsServiceImpl.class);

    @Resource(name = "messageImmediatecodeServiceImpl")
    private ISunriseService<StringInput> messageImmediatecodeServiceImpl;


    private boolean sendNotifySms(String mobile, String errorMessage, String messageId, String businessType, String companyName) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("appId", 1);
            jsonObject.put("smsTemplate", "SMS_155551202");
            jsonObject.put("mobile", mobile);
            jsonObject.put("smsPurposeType", 2);

            JSONObject fields = new JSONObject();
            fields.put("dateStr", DateUtil.format(new Date(), "MM-dd HH:mm:ss"));
            fields.put("errorType", errorMessage);
            fields.put("messageId", messageId);
            fields.put("businessType", businessType);
            fields.put("companyName", companyName);
            jsonObject.put("fields", fields);

            StringInput smsInput = new StringInput(jsonObject.toJSONString());
            Integer sendResult = (Integer) messageImmediatecodeServiceImpl.execute(null, smsInput);
            return (null != sendResult && sendResult > 0);
        } catch (Exception e) {
            logger.error("发送短信通知失败, mobile="+mobile + ", messageId="+messageId + ", businessType="+businessType + ", errorMessage="+errorMessage, e);
            return false;
        }
    }

    public void sendNotifySms(String errorMessage, String messageId, String businessType, String companyName, List<String> notifyMobileList) {
        for (String notifyMobile : notifyMobileList) {
            sendNotifySms(notifyMobile, errorMessage, messageId, businessType, companyName);
        }
    }

}
